package com.offcn.bigdata.spark.p2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class GroupTopNMRApp  {
    public static void main(String[] args) throws Exception {
        if(args == null || args.length < 2) {
            System.err.println("Usage: <input> <output>");
            System.exit(-1);
        }
        String input = args[0];
        String output = args[1];

        Configuration conf = new Configuration();
        String jobName = GroupTopNMRApp.class.getSimpleName();
        Job job = Job.getInstance(conf, jobName);

        //map-->input
        job.setInputFormatClass(TextInputFormat.class);
        FileInputFormat.setInputPaths(job, input);
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //reducer-->output
        job.setOutputFormatClass(TextOutputFormat.class);
        FileOutputFormat.setOutputPath(job, new Path(output));
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //task个数
        job.setNumReduceTasks(1);

        job.waitForCompletion(true);
    }

    static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            //chinese ls 91
            String[] fields = value.toString().split("\\s+");
            if(fields == null || fields.length != 3) {
                return;
            }
            String course = fields[0];
            String name = fields[1];
            double score = Double.valueOf(fields[2]);
            context.write(new Text(course), new Text(name + "|" + score));
        }
    }

    static class MyReducer extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            TreeSet<String> ts = new TreeSet<String>(new Comparator<String>() {
               @Override
               public int compare(String o1, String o2) {
                   Double score1 = Double.valueOf(o1.split("\\|")[1]);
                   Double score2 = Double.valueOf(o2.split("\\|")[1]);
                   return score2.compareTo(score1);
               }
           });
           int size = 3;
           for(Text text: values) {
               ts.add(text.toString());
               if(ts.size() >3) {
                   ts.pollLast();
               }
           }

           context.write(key, new Text(ts.toString()));
        }
    }
}
